/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.al;

import java.util.Map;

import com.google.common.annotations.VisibleForTesting;

import edu.byu.nlp.pipes.Instance;

/**
 * An InstanceProvider that assigns the annotator to the next instance with the least number of annotations. 
 * 
 * @author rah67
 *
 */
public class LeastAnnotationsInstanceProvider<L, D> implements InstanceManager<L, D> {

	private static class ProhibitDuplicatesQueue<L, D> {

		private final Instance<L, D>[] instances;
		private int curIndex;
		private final Map<Long, Integer> lastAccessedMap;
		
		public synchronized Instance<L, D> take(long annotatorId) {
			int index;
			// Makes sure key exists
			int lastAccessedIndex = lastAccessedMap.get(annotatorId);
			if (curIndex < lastAccessedIndex) {
				index = lastAccessedIndex + 1;
			} else {
				index = curIndex++;
			}
			// Could allow others access to curIndex
			// could tighten locks around map (or use ConcurrentHashMap)
			
			// Need to guard the annotations multi-map
			while (instances[index].getAnnotations().containsKey(annotatorId)) {
				++index;
			}
			lastAccessedMap.put(annotatorId, index);
			return instances[index];
		}
	}
	
	
	private final ProhibitDuplicatesQueue<L, D> q;
	
	@VisibleForTesting
	LeastAnnotationsInstanceProvider(ProhibitDuplicatesQueue<L, D> q) {
		this.q = q;
	}
	
	public static <L, D> LeastAnnotationsInstanceProvider<L, D> newProvider(Iterable<Instance<L, D>> instances,
			int initialCapacity) {
	}

	/** {@inheritDoc} */
	@SuppressWarnings("unchecked")
	@Override
	public AnnotationRequest<L, D> requestInstanceFor(long annotatorId) {
		return q.take(annotatorId);
	}
	
	private class MyRequest extends BaseInstanceResponse<L, D> {

		public MyRequest(long annotatorId, Instance<L, D> instance) {
			super(annotatorId, instance);
		}

		/** {@inheritDoc} */
		@Override
		public void cancelRequest() {
			// If the request is cancelled, simply add the instance back to the queue
			try {
				q.put(instance);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}

		/** {@inheritDoc} */
		@Override
		public boolean storeAnnotation(L annotation) {
			// Update the instance with the new annotation for this annotator and put back on queue
			try {
				q.put(instance);
			} catch (InterruptedException e) {
				return false;
			}
			return instance.getAnnotations().put(annotatorId, annotation);
		}
		
	}
	
	private AnnotationRequest<L, D> newRequest(long annotatorId, Instance<L, D> instance) {
		return new MyRequest(annotatorId, instance);
	}
	
}
